package com.vanzo.talkie2;

import android.app.Activity;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2018/1/16.
 */

public class CxcssPicker {
    private static final String TAG = "CxcssPicker";
    private static List<String> cssList1;
    private static List<String> cssList2;

    static {
        cssList1 = new ArrayList<>();
        cssList2 = new ArrayList<>();
        cssList1.add("无编码");
        cssList1.add("CTCSS");
        cssList1.add("DCS");
        for (int i = 0; i <= 121; i++) {
            cssList2.add(i < 10 ? "0" + i : "" + i);
        }
    }


    private DoublePicker picker;
    private Activity activity;
    private ColorTextView textView;
    private int rStringId;
    private String defaultValue;

    public CxcssPicker(Activity activity, ColorTextView textView, int rStringId, String defaultValue) {
        this.activity = activity;
        this.textView = textView;
        this.rStringId = rStringId;
        this.defaultValue = defaultValue;
        this.textView.setTitle(rStringId);
        this.textView.setValue(defaultValue);
        this.textView.setOnClickListener(textViewListener);
    }

    private View.OnClickListener textViewListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (picker == null) {
                picker = new DoublePicker(activity, cssList1, cssList2);
                picker.setOnPickListener(new DoublePicker.OnPickListener() {
                    @Override
                    public void onPicked(int var1, int var2) {
                        textView.setValue(cssList2.get(var2));
                    }
                });
                picker.setOnWheelListener(new DoublePicker.OnWheelListener() {
                    @Override
                    public void onFirstWheeled(int var1, String var2) {
                        switch (var1) {
                            case 0:
                                picker.setSelectedSecondIndex(0);
                                break;
                            case 1:
                                if (picker.getSelectedSecondIndex() == 0) {
                                    picker.setSelectedSecondIndex(1);
                                } else if (picker.getSelectedSecondIndex() > 38) {
                                    picker.setSelectedSecondIndex(38);
                                }
                                break;
                            case 2:
                                if (picker.getSelectedSecondIndex() < 39) {
                                    picker.setSelectedSecondIndex(39);
                                }
                                break;
                        }
                    }

                    @Override
                    public void onSecondWheeled(int var1, String var2) {
                        if (var1 == 0) {
                            picker.setSelectedFirstIndex(0);
                        } else if (var1 <= 38) {
                            picker.setSelectedFirstIndex(1);
                        } else {
                            picker.setSelectedFirstIndex(2);
                        }
                    }
                });

            }
            picker.show();
            String defaultValue = textView.getValue().toString();
            picker.setSelectedIndex(cssList2.indexOf(defaultValue) == 0 ? 0 : (cssList2.indexOf(defaultValue) <= 38 ? 1 : 2), cssList2.indexOf(defaultValue));
        }
    };
}
